from pymongo import MongoClient
import bson
import re
client = MongoClient(host="mongodb://127.0.0.1",port=27017)
db = client['book']['name']
# data = db.insert({'name':44})
# print(data)

# data_2 = db.find()
# print([i for i in data_2])

# var_persons = [{
# 'name':"jim",
# 'age':25,
# 'email':"75431457@qq.com",
# 'c':89,'m':96,'e':87,
# 'country':"USA",
# 'books':["JS","C++","EXTJS","MONGODB"]
# },
# {
# 'name':"tom",
# 'age':25,
# 'email':"214557457@qq.com",
# 'c':75,'m':66,'e':97,
# 'country':"USA",
# 'books':["PHP","JAVA","EXTJS","C++"]
# },
# {
# 'name':"lili",
# 'age':26,
# 'email':"344521457@qq.com",
# 'c':75,'m':63,'e':97,
# 'country':"USA",
# 'books':["JS","JAVA","C#","MONGODB"]
# },
# {
# 'name':"zhangsan",
# 'age':27,
# 'email':"2145567457@qq.com",
# 'c':89,'m':86,'e':67,
# 'country':"China",
# 'books':["JS","JAVA","EXTJS","MONGODB"]
# },
# {
# 'name':"lisi",
# 'age':26,
# 'email':"274521457@qq.com",
# 'c':53,'m':96,'e':83,
# 'country':"China",
# 'books':["JS","C#","PHP","MONGODB"]
# },
# {
# 'name':"wangwu",
# 'age':27,
# 'email':"65621457@qq.com",
# 'c':45,'m':65,'e':99,
# 'country':"China",
# 'books':["JS","JAVA","C++","MONGODB"]
# },
# {
# 'name':"zhaoliu",
# 'age':27,
# 'email':"214521457@qq.com",
# 'c':99,'m':96,'e':97,
# 'country':"China",
# 'books':["JS","JAVA","EXTJS","PHP"]
# },
# {
# 'name':"piaoyingjun",
# 'age':26,
# 'email':"piaoyingjun@uspcat.com",
# 'c':39,'m':54,'e':53,
# 'country':"Korea",
# 'books':["JS","C#","EXTJS","MONGODB"]
# },
# {
# 'name':"lizhenxian",
# 'age':27,
# 'email':"lizhenxian@uspcat.com",
# 'c':35,'m':56,'e':47,
# 'country':"Korea",
# 'books':["JS","JAVA","EXTJS","MONGODB"]
# },
# {
# 'name':"lixiaoli",
# 'age':21,
# 'email':"lixiaoli@uspcat.com",
# 'c':36,'m':86,'e':32,
# 'country':"Korea",
# 'books':["JS","JAVA","PHP","MONGODB"]
# },
# {
# 'name':"zhangsuying",
# 'age':22,
# 'email':"zhangsuying@uspcat.com",
# 'c':45,'m':63,'e':77,
# 'country':"Korea",
# 'books':["JS","JAVA","C#","MONGODB"]
# }]
# for i in var_persons:
#     data_4 = db.insert(i)

#查询年龄在25到27之间的学生
# data_5 = db.find({'age':{'$lte':27,'$gte':25}},{'_id':0,'name':1,'age':1})
# for i in data_5:
#     print(i)

#查询不是韩国学生的数字成绩机姓名：
# data_6 = db.find({'country':{'$ne':'Korea'}},{'_id':0,'name':1,'m':1})
# for i in data_6:
#     print(i)

#查询出国籍是USA或China的学生姓名：
# data_7 = db.find({'$or':[{'country':'China'},{'country':'USA'}]},{'_id':0,'name':1})
# for i in data_7:
#     print(i)

# data_8 = db.find({'country':{'$nin':['China','USA']}},{'_id':0,'name':1})
# for i in data_8:
#     print(i)                   #nin只能在数组里面的，not可以在任何地方

#查询语文成绩大于85或者英语成绩大于90的学生
# data_9 = db.find({'$or':[{'c':{'$gt':85}},{'e':{'$gt':90}}]},{'_id':0,'name':1,'c':1,'e':1})
# for i in data_9:
#     print(i)

#给country是中国的学生加上sex:'m'
# data_10 = db.update_many({'country':'China'},{'$set':{'sex':'m'}})
# print(data_10)

#查询sex是null的学生姓名和国籍
# data_11 = db.find({'sex':{'$in':[None]}},{'_id':0,'name':1,'country':1})
# for i in data_11:
#     print(i)

#查询名字中存在li的同学
# patt = re.compile('li')
# content = bson.Regex.from_native(patt)    #转换正则表达式
# data_12 = db.find({'name':content},{'_id':0,'name':1})
# for i in data_12:
#     print(i)

#查询名字中不存在li的同学
# patt = re.compile('li')
# content = bson.Regex.from_native(patt)    #转换正则表达式
# data_13 = db.find({'name':{'$not':content}},{'_id':0,'name':1})
# for i in data_13:
#     print(i)

#查询喜欢JS和MONDODB的学生
# data_14 = db.find({'books':{'$all':['JS','MONGODB']}},{'_id':0,'name':1})
# for i in data_14:
#     print(i)

#查询喜欢第二本书是‘JAVA’的学生
# data_15 = db.find({'books.1':'JAVA'},{'_id':0,'name':1})
# for i in data_15:
#     print(i)


#查询喜欢书的数量是4的学生
# data_16 = db.find({'books':{'$size':4}},{'_id':0,'name':1})
# for i in data_16:
#     print(i)

#给所有的文档加一个size:4
# data_17 = db.update_many({},{'$set':{'size':4}})

#查询JIM喜欢的书的数量
# data_16 = db.find({'name':'jim'},{'_id':0,'books':1})
# for i in data_16:
#     for x in i.values():
#         print(len(x))

